<?php

ini_set('include_path', '../');

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");

require_once("util/configuration_parametters.php");

require_once("model/article/to/article_to.php");
require_once("model/article/dao/article_dao_factory.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");

require_once("model/article_description/dao/article_description_dao_factory.php");
require_once("model/article_category/dao/article_category_dao_factory.php");
require_once("model/Article_article_category/dao/article_article_category_dao_factory.php");
require_once("model/article_description/to/article_description_to.php");
require_once("model/article_category/to/article_category_to.php");
require_once("model/Article_article_category/to/Article_article_category_to.php");

class Article_dao_test {
	
	private $dao;
	private $autor;

	public function __construct() {		
	}
	
	public function before_test() {
		$this->dao = Autor_dao_factory::singleton()->get_dao();
		$this->autor = new Autor_to('xxxxxxxxxxxx','password','email');
		$this->dao->create($this->autor);
		try {
			$this->dao = Article_dao_factory::singleton()->get_dao();
		}
		catch (Internal_error_exception $e) {
			$e->getMessage() . '</br>';
		}
		 	
	}
 
	public function run_test() {
		try {
			$date = date("Y-m-d H:i:s");
			$article_to = new Article_to(0, 'title', $this->autor->get_login(), $date, 'open', 0);
			
			//create
			$article_to = (object) $this->dao->create($article_to);	
			echo 'create article: ' . $article_to->to_string() . '<br/>';
			
			//create2
			$article_to2 = new Article_to(0, 'title3', $this->autor->get_login(), $date, 'open', 0);
			$article_to2 = (object) $this->dao->create($article_to2);	

			//create existent 
			try {
				$this->dao->create($article_to);
				throw new Exception('create duplicate');
			}
			catch (Duplicate_instance_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//exist inexistent 
			if (!$this->dao->exist(9999))
				echo 'No exist article 9999<br/>';
			else 
				throw new Exception("Error, exist article inexistent");
			
			//exist existent 
			$this->dao->exist($article_to->get_number());
			echo 'exist article: ' . $article_to->to_string() . '<br/>';
			
			//find inexistent 
			try {
				$this->dao->find_by_number_article(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
	
			//find existent 
			$aux_article_to = $this->dao->find_by_number_article($article_to->get_number());	
				if ($article_to->equals($aux_article_to)) 
					echo 'found article: ' . $article_to->to_string() . '<br/>';
				else
					echo 'not equal article inserted';

			//find most recent
			$article_list = $this->dao->find_most_recent(0, 2);
				echo 'Most recent:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';
				
			//find most viewed
			$article_list = $this->dao->find_most_viewed(1,2);
				echo 'Most viewed:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';
			
			//find by keywords title_and
			$keywords[0] = 'tit';
			$keywords[1] = '3';
			$article_list = $this->dao->find_by_keywords_title_and($keywords, 0, 2);
				echo 'Serach by keyword title and:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';
				
			//find by keywords title_or
			$article_list = $this->dao->find_by_keywords_title_or($keywords, 0, 2);
				echo 'Serach by keyword title or:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';

			//Inserts aux
			$article_to3 = new Article_to(0, 'pepete', $this->autor->get_login(), $date, 'open', 0);
			$article_to3 = (object) $this->dao->create($article_to3);	
			$dao_aux = Article_description_dao_factory::singleton()->get_dao();
			$dao_aux->create(new Article_description_to(0, 'content3', $article_to3->get_number(), 
									$this->autor->get_login(), $date, 'open'));
			$dao_aux->create(new Article_description_to(0, 'titcontent3', $article_to2->get_number(), 
									$this->autor->get_login(), $date, 'open'));									
			$dao_aux = Article_category_dao_factory::singleton()->get_dao();
			$article_category = $dao_aux->create(new Article_category_to(0,'title', 'description', 'xxxxxxxxxxxx', 0));
			$article_category2 = $dao_aux->create(new Article_category_to(0,'title 3', 'description', 'xxxxxxxxxxxx', 0));
			$dao_aux = Article_article_category_dao_factory::singleton()->get_dao();
			$article_article_category_to = new Article_article_category_to($article_to3->get_number(), $article_category->get_number());
			$article_article_category2_to = new Article_article_category_to($article_to2->get_number(), $article_category2->get_number());
			$dao_aux->create($article_article_category_to->get_article_number(), 
									$article_article_category_to->get_article_category_number());
			$dao_aux->create($article_article_category2_to->get_article_number(), 
									$article_article_category2_to->get_article_category_number());
									
				
			//find by keywords title_description_and
			$article_list = $this->dao->find_by_keywords_title_description_and($keywords, 0, 2);
				echo 'Serach by keyword title description and:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';
			
			//find by keywords title_description_or
			$article_list = $this->dao->find_by_keywords_title_description_or($keywords, 0, 2);
				echo 'Serach by keyword title description or:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';
				
			//find by keywords title_category_and
			$article_list = $this->dao->find_by_keywords_title_category_and($keywords, 0, 2);
				echo 'Serach by keyword title category and:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';
				
			//find by keywords title_category_or
			$article_list = $this->dao->find_by_keywords_title_category_or($keywords, 0, 3);
				echo 'Serach by keyword title category or:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';
			
			//find by keywords title_description_category_and
			$article_list = $this->dao->find_by_keywords_title_description_category_and($keywords, 0, 3);
				echo 'Serach by keyword title description category and:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';
				
			//find by keywords title_description_category_and
			$article_list = $this->dao->find_by_keywords_title_description_category_or($keywords, 0, 3);
				echo 'Serach by keyword title description category or:</br>';
				foreach($article_list as $article)
					echo '    Article: ' . $article->to_string() . '</br>';
				
			//count articles by autor
			$articles = $this->dao->count_articles_by_login($this->autor->get_login());
			echo 'Count articles by autor: ' . $articles . '</br>';
			
			//update 
			$article_to->set_title("title2");
			$this->dao->update($article_to);
			$aux_article_to = $this->dao->find_by_number_article($article_to->get_number());
			echo 'update article: ' . $aux_article_to->to_string() . '<br/>';

			//remove inexistent 	
			try {
				$this->dao->remove(9999);
				throw new Exception('Removed inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '<br/>';
			}
			
			//remove existent 
			$this->dao->remove($article_to->get_number());
			echo 'remove article: ' . $article_to->to_string() . '<br/>';

		}
		catch (Duplicate_instance_exception $e) {
			echo $e->getMessage();
		}
		catch (Internal_error_exception $e) {
			echo $e->getMessage();
		}
		catch (Instance_not_found_exception $e) {
			echo $e->getMessage();
		}
		catch (Exception $e) {
			echo $e->getMessage();
		}
		
	}
	
	function after_test() {
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor->get_login());
	}
}



//Test
$test = new Article_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();
							

?>